import Vue from "vue";
import VueRouter from "vue-router";
import route_lists from './route_list';
import store from '../store';
Vue.use(VueRouter);

const routes = [
   ...route_lists
];

const router = new VueRouter({
   mode: "hash",           // hash 加#       history   /
   base: process.env.BASE_URL,
   routes
});

//  路由守卫配置
router.beforeEach((to, from, next) => {
   const store_ = localStorage.getItem('store') ? JSON.parse(localStorage.getItem('store')).token || '' : '',
   authority = store.state.authority || {};
   let { path, name } = to || {};
   authority.hasOwnProperty(path) ? to.query.check = authority[path] : to.query.check = {};
   if (to.name == 'login') {
      next();
   } else {
      if (store_ && store_ != null && store_ != undefined) {
         next()
      } else {
         next('/login')
      }
   }
})
router.afterEach((vm) => {
   store.commit("changeState", { nav_titleList: vm.meta.route_list || [] });
   // NProgress.done()
})

export default router;
